import Vue from 'vue'
import Router from 'vue-router'
// 导入布局组件
import Main from '../components/Main'
//导入登录组件
import Login from '../components/Login'
//欢迎组件
import Welcome from '../components/Welcome'
import User from '../components/table/User'
import Anno from '../components/table/Anno'
import Hospital from '../components/table/Hospital'
import Msgbox from '../components/table/MsgBox'
import VaccineSort from'../components/table/VaccineSort'
import Vaccine from'../components/table/Vaccine'
import VaccineOrder from'../components/table/VaccineOrder'
import VaccineInfo from'../components/table/VaccineInfo'
//启用路由，脚手架自动生成代码
Vue.use(Router)

let router = new Router({
  routes: [
    { //定义多个路由
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      component: Login,
    },
    {
      path: '/main',
      name: 'Main',
      component: Main,
      redirect: '/welcome',
      children: [ //定义子组件
        {
          path: '/welcome',
          component: Welcome
        },
        {
          path: '/hospital',
          name: 'hospital',
          component:Hospital
        },
        {
          path: '/user',
          name: 'user',
          component:User
        },
        {
          path: '/msgbox',
          name: 'msgbox',
          component: Msgbox
        },
        {
          path: '/anno',
          name: 'anno',
          component: Anno
        },
        {
          path: '/vaccineSort',
          name: 'vaccineSort',
          component: VaccineSort
        },
        {
          path: '/vaccine',
          name: 'vaccine',
          component: Vaccine
        },
        {
          path: '/vaccineOrder',
          name: 'vaccineOrder',
          component: VaccineOrder
        },
        {
          path: '/vaccineInfo',
          name: 'vaccineInfo',
          component: VaccineInfo
        }
      ]
    }
  ]
})


// 定义一个路由器
router.beforeEach((to, from, next) => {
  if (to.path == '/login') {
    next();
  } else {
    if (localStorage.getItem("token")) {
      next();
    } else {
      alert('该用户还未登录，请登录')
      next({
        path: '/login'
      })
    }
  }
})
export default router
